layui.use(['form','layer','tree'],function (){
    let form = layui.form,layer = layui.layer,tree = layui.tree;
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 初始化回显新增信息
*/
$(function (){
    initRoleId()
    initRoleCode()
    initRolePermList();
});

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 放弃按钮
*/
function quitInsertRoleInfo() {
    //获取当前弹窗的索引，并关闭；parent 是 layui 定义的全局变量
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 新增用户信息
*/
function insetRoleInfo() {
    //角色编号
    let roId = $("#roId").val().trim();
    //角色代码
    let roNm = $("#roNm").val();
    //角色名称
    let roDesc = $("#roDesc").val().trim();
    //所属机关
    let roCode = $("#roCode").val().trim();
    //权限信息
    let list = getPermIdList(layui.tree.getChecked('treeId'));

    if (valueIsEmpty(roNm) || valueIsEmpty(roDesc)){
        layer.msg("角色代码或角色名称不能为空!",{icon:0,time: 1500});
        return;
    }
    if (valueIsEmpty(list)){
        layer.msg("角色菜单权限信息不能为空!",{icon:0,time: 1500});
        return;
    }
    let roleJsonData = {
        "roId" : roId,
        "roNm" : roNm,
        "roDesc" : roDesc,
        "roCode" : roCode,
        "list" : list
    }
    sendAjax("/systemDefend/roleAdmin/insertRoleInfo",roleJsonData,function (result){
        if (valueIsEqual(result.code,"00")){
            quitInsertRoleInfo();
        }else if (valueIsEqual(result.code,"02")){
            layer.msg(result.data,{icon:5,time: 1500});
        }
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/14
* @description: 初始化用户单位信息
*/
function initRoleCode() {
    sendAjax("/systemDefend/roleAdmin/initRoleCode",null,function (result) {
        if (valueIsEqual("00",result.code)){
            $("#roCode").val(result.data);
        }
    })
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 初始化返显权限列表
*/
function initRolePermList() {
    sendAjax("/systemDefend/roleAdmin/showAllRolePermInfo",null,function (result) {
        if (valueIsEqual("00",result.code)){
            layui.tree.render({
                elem: '#tree',
                id:'treeId',
                showCheckbox: true,
                data: result.data
            });
        }
    })
}

/**
 * @author: ChengJW
 * @params:
 * @return:
 * @date: 2021/11/11
 * @description: 初始化角色编码
 */
function initRoleId() {
    sendAjax("/systemDefend/roleAdmin/initRoleId",null,function (result) {
        if (valueIsEqual("00",result.code)){
           $("#roId").val(result.data);
        }
    });
}

function getPermIdList(data){
    let id = "";
    $.each(data, function (index, item) {
        if (!valueIsEmpty(id)) {
            id = id + "," + item.id;
        }
        else {
            id = item.id;
        }
        //item 没有children属性
        if (item.children != null) {
            var i = getPermIdList(item.children);
            if (!valueIsEmpty(i)) {
                id = id + "," + i;
            }
        }
    });
    return id;
}